﻿
Imports centrosphere

Partial Public Class storageJobDistributor
    Protected Class redundance_compare
        Private Shared Function result_compare(ByVal this As storageJob,
                                               ByVal that As storageJob) As Int32
            compare_assert(this, that)
            If this.result = that.result Then
                Return 0
            ElseIf this.result Then
                Return 1
            Else
                Return -1
            End If
        End Function

        Public Shared Function result_compare(ByVal this As ijob,
                                              ByVal that As ijob) As Int32
            Return result_compare(storageJob(this), storageJob(that))
        End Function

        Private Shared Function i64_compare(ByVal this As storageJob,
                                            ByVal that As storageJob) As Int32
            compare_assert(this, that)
            Dim cmp As Int32 = 0
            cmp = result_compare(this, that)
            If cmp = 0 AndAlso this.result Then
                Dim diff As Int64 = 0
                diff = this.i64 - that.i64
                If diff > 0 Then
                    Return 1
                ElseIf diff < 0 Then
                    Return -1
                Else
                    Return 0
                End If
            Else
                Return cmp
            End If
        End Function

        Public Shared Function i64_compare(ByVal this As ijob,
                                           ByVal that As ijob) As Int32
            Return i64_compare(storageJob(this), storageJob(that))
        End Function

        Private Shared Function buff_compare(ByVal this As storageJob,
                                             ByVal that As storageJob) As Int32
            compare_assert(this, that)
            Dim cmp As Int32 = 0
            cmp = i64_compare(this, that)
            If cmp = 0 AndAlso this.result Then
                If this.buff Is Nothing AndAlso that.buff Is Nothing Then
                    Return 0
                ElseIf this.buff Is Nothing Then
                    Return -1
                ElseIf that.buff Is Nothing Then
                    Return 1
                Else
                    Return this.buff.size() - that.buff.size()
                End If
            Else
                Return cmp
            End If
        End Function

        Public Shared Function buff_compare(ByVal this As ijob,
                                            ByVal that As ijob) As Int32
            Return buff_compare(storageJob(this), storageJob(that))
        End Function

        Public Shared Function read_compare(ByVal this As ijob,
                                            ByVal that As ijob) As Int32
            Return buff_compare(this, that)
        End Function

        Public Shared Function seek_compare(ByVal this As ijob,
                                            ByVal that As ijob) As Int32
            Return result_compare(this, that)
        End Function

        Public Shared Function sizeof_compare(ByVal this As ijob,
                                              ByVal that As ijob) As Int32
            Return i64_compare(this, that)
        End Function

        Public Shared Function modify_compare(ByVal this As ijob,
                                              ByVal that As ijob) As Int32
            Return result_compare(this, that)
        End Function

        Public Shared Function write_compare(ByVal this As ijob,
                                             ByVal that As ijob) As Int32
            Return result_compare(this, that)
        End Function

        'if a read is following a uniqueWrite command,
        'but uniqueWrite.sync_read_modify has not been called, the data will be wrong.
        Public Shared Function uniqueWrite_compare(ByVal this As ijob,
                                                   ByVal that As ijob) As Int32
            Return -result_compare(this, that)
        End Function
    End Class
End Class
